# $Id: test-common 215 2006-08-15 10:22:59Z sfsetse $
# vim:syntax=sh:sw=2:ts=2:expandtab
#
# Author: Kate Ward <kate.ward@forestent.com>
#
# The spiffy Simsons comments were shamelessly pulled from the lf5 examples
# from log4j.
#

#
# setup the initial logging environment
#

smtpEmail="postmaster@localhost"
syslogFacility="local4"

#
# do some logging
#

# log some at the DEBUG level
logger_debug "Hello, my name is Homer Simpson."
logger_debug "Hello, my name is Lisa Simpson."
logger_debug "Hello, my name is Marge Simpson."
logger_debug "Hello, my name is Bart Simpson."
logger_debug "Hello, my name is Maggie Simpson."

# log some at the INFO level
logger_info "We are the Simpsons!"
logger_info "Mmmmmm .... Chocolate."
logger_info "Homer likes chocolate"
logger_info "Doh!"
logger_info "We are the Simpsons!"

# log some at the WARN level
logger_warn "Bart: I am through with working! Working is for chumps!\
 Homer: Son, I'm proud of you. I was twice your age before\
 I figured that out."
logger_warn "Mmm...forbidden donut."
logger_warn "D'oh! A deer! A female deer!"
logger_warn "Truly, yours is a butt that won't quit.\
 - Bart, writing as Woodrow to Ms. Krabappel."

# set the level of the mySTDERR appender to OFF
appender_setLevel mySTDERR OFF

# log some at the ERROR level
logger_error "Dear Baby, Welcome to Dumpsville. Population: you."
logger_error "Mr. Hutz, are you aware you're not wearing pants?"

# set the mySTDERR level to DEBUG
appender_setLevel mySTDERR DEBUG

# log some at the FATAL level
logger_fatal "Eep."
logger_fatal "Mmm...forbidden donut."
logger_fatal "D'oh! A deer! A female deer!"
logger_fatal "Mmmmmm .... Chocolate."

# various setting changes
appender_setLayout mySTDERR PatternLayout
logger_info "test of the default pattern layout"

appender_setPattern mySTDERR '%d [%F] %p - %m%n'
logger_info "test of setting a pattern layout"

appender_setPattern mySTDERR '%d %p - %m%n'
logger_info "test of setting a pattern layout again"

appender_setPattern mySimple '%d [%F] %p - %m'
logger_info "setting a pattern for a simple layout logger.  should not change the pattern of the simple logger as it is still set to a simple layout."

# manually set the value of the filename flag
logger_setFilename "myFilename"
appender_setPattern myPattern '%d [%F] %p - %m%n'
logger_info "setting a new pattern for a patterned layout file logger"

appender_setPattern myPattern '%d [%f] %p - %m%n'
logger_info "setting a pattern for a patterned layout file logger, using an invalid character conversion."

appender_setPattern myPattern '%d %p - %m%n'
logger_info "setting the pattern for the patterned layout file logger back to a reasonable value."

# close some appenders
logger_info "closing the myFilename appender"
appender_close myFilename

# test the *ThreadName functions
appender_setPattern mySTDERR '%p [%F:%t]: %m%n'
logger_info "setting a pattern layout for STDERR that uses the thread name conversion character.  the thread name isn't set yet, so it should be the default value ('main' as of this writing)..."
logger_setThreadName myThread
logger_info "the thread name is now set to the name of this script, so it should show up in output now"
logger_info "will now 'push' a new thread 'myNewThread' on the thread name stack..."
logger_pushThreadName "myNewThread"
logger_info "checking to see if the thread name changed.  did it?"
thread=`logger_getThreadName`
logger_info "just 'got' the thread name of '$thread'.  does it match what is shown over on the left?"
logger_pushThreadName "mySecondNewThread"
logger_pushThreadName "myThirdNewThread"
logger_info "just pushed two threads named 'mySecondNewThread' and 'myThirdNewThread'.  verify that the later is visible on the left, and that they pop off correctly."
for f in 1 2; do
  thread=`logger_getThreadName`
  logger_popThreadName
  logger_info "popped the thread named '${thread}'"
done
logger_popThreadName
logger_info "I just popped the topmost thread name.  the first thread name should be shown again.  is it?"
logger_popThreadName
logger_info "I just tried popping again.  the thread name should not have changed as we were already at the topmost thread name.  there should have been a log4sh warning as well."

# test the smtp appender
logger_addAppender mySMTP
appender_setAppenderType mySMTP SMTPAppender
appender_setLevel mySMTP FATAL
appender_smtp_setSubject mySMTP "test subject"
appender_smtp_setTo mySMTP $smtpEmail
logger_info "this message *should not* generate an email."
logger_fatal "this message *should* generate an email to '$smtpEmail'."
appender_close mySMTP

# test the extra syslog appender functions
syslogFacilityResult=`appender_syslog_getFacilityByName mySyslog`
logger_info "just requested the syslog facility for the mySyslog appender.  I expected '$syslogFacility' and got '$syslogFacilityResult'.  do they match?"
logger_info "closing the mySyslog appender"
appender_close mySyslog

# test the sending of multiple options to a logger function
logger_info this is a test message without quotes to verify that a logging function can be called without wrapping the message in quotes.

# testing %X conversion character - MDC (Mapped Diagnostic Context)
logger_info "the next test tests the %X and %% conversion characters"
appender_setPattern mySTDERR '%d %p [%X{percent}%%] - %m%n'
percent=30
logger_info "got some work done"
percent=75
logger_info "got some work more done"
percent=100
logger_info "our work is done"
appender_setPattern mySTDERR '%d %p - %m%n'

# workaround implementation of separated date and time
logInfo() { mdcD=`date "+%Y-%m-%d"`;mdcT=`date "+%H:%M:%S"`;logger_info "$@"; }

appender_setPattern mySTDERR '%X{mdcD}|%X{mdcT}|%m%n'
logInfo "MDC test including the date/time"
appender_setPattern mySTDERR '%d %p - %m%n'

# test proper parsing of the '\' and '&' characters
logger_info "this message should have a backslash character hier >\<"
logger_info "this message should have an andpersand character hier >&<"

# test grabbing output from some various commands
logger_info "this system's uname: `uname -a`"
logger_info "output of 'ls -l': `ls -l`"

# test sending things to log4sh via pipes
echo "if this message is seen, then the sending of a message via a pipe works" |logger_info
# try the same 'ls -l' like above, but with a pipe
logger_info "output of 'ls -l' [again] via pipe in next logging statement..."
ls -l |logger_info

# test the calling of the previous trap handler
logger_info "after this message, you should see a non-log4sh message that a cleanup function was called.  if you do, than you know that the trap handler of this test script was called, and that log4sh was able to properly call that handler upon exit."
